/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.fastcase.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.fastcase.enums.AppraiseTemplateItemTypeEnum;
import com.icinfo.cloud.provider.punish.fastcase.mapper.CaseAppraiseTemplateDetailMapper;
import com.icinfo.cloud.provider.punish.fastcase.mapper.CaseAppraiseTemplateInfoMapper;
import com.icinfo.cloud.provider.punish.fastcase.model.CaseAppraiseTemplateDetail;
import com.icinfo.cloud.provider.punish.fastcase.model.CaseAppraiseTemplateInfo;
import com.icinfo.cloud.provider.punish.fastcase.service.ICaseAppraiseTemplateInfoService;
import com.icinfo.cloud.provider.punish.fastcase.vo.*;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述: 案件评价模板表 case_appraise_template_info 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2023年02月02日
 */
@Slf4j
@Service
public class CaseAppraiseTemplateInfoServiceImpl extends MyBatisServiceSupport implements ICaseAppraiseTemplateInfoService {

    @Resource
    private CaseAppraiseTemplateInfoMapper caseAppraiseTemplateInfoMapper;
    @Resource
    private CaseAppraiseTemplateDetailMapper caseAppraiseTemplateDetailMapper;

    /**
     * 描述： 根据案件类型获取评价模板
     *
     * @author: zhengqiang
     * @date: 2023/2/3
     * @param: caseType
     * @return {@link List< CaseAppraiseTemplateGroupInfoVo>}
     **/
    @Override
    public List<CaseAppraiseTemplateGroupInfoVo> getCaseAppraiseTemplateInfo(String caseType) {
        List<CaseAppraiseTemplateGroupInfoVo> templateGroupInfoList = new ArrayList<>();

        Example example = new Example(CaseAppraiseTemplateInfo.class);
        example.createCriteria().andEqualTo("applyCaseType", caseType).andEqualTo("isDelete", Constant.STRING_0);
        List<CaseAppraiseTemplateInfo> caseAppraiseTemplateInfos = caseAppraiseTemplateInfoMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(caseAppraiseTemplateInfos)) {
            throw new BusinessException("案件评价模板不存在");
        }
        CaseAppraiseTemplateInfo caseAppraiseTemplateInfo = caseAppraiseTemplateInfos.get(0);

        List<CaseAppraiseTemplateDetailGroupVo> caseAppraiseTemplateDetailGroupInfo = caseAppraiseTemplateDetailMapper.getCaseAppraiseTemplateDetailGroupInfo(caseAppraiseTemplateInfo.getId());
        if (ObjectUtils.isEmpty(caseAppraiseTemplateDetailGroupInfo)) {
            throw new BusinessException("案件评价模板详情未配置");
        }
        for (CaseAppraiseTemplateDetailGroupVo groupVo : caseAppraiseTemplateDetailGroupInfo) {
            Integer groupSort = groupVo.getGroupSort();
            CaseAppraiseTemplateGroupInfoVo groupInfoVo = new CaseAppraiseTemplateGroupInfoVo();
            groupInfoVo.setGroupName(groupVo.getGroupName());

            List<Object> groupContent = new ArrayList<>();
            List<CaseAppraiseTemplateDetail> caseAppraiseTemplateDetailItemByGroup = caseAppraiseTemplateDetailMapper.getCaseAppraiseTemplateDetailItemByGroup(groupSort);
            for (CaseAppraiseTemplateDetail templateDetail : caseAppraiseTemplateDetailItemByGroup) {

                CaseAppraiseTemplateItemVo itemVo = new CaseAppraiseTemplateItemVo();
                itemVo.setTage(templateDetail.getItemType());
                itemVo.setLabel(templateDetail.getItemLabel());
                itemVo.setValue("value" + templateDetail.getItemSort());
                if (!ObjectUtils.isEmpty(templateDetail.getItemRules())) {
                    List<JSONObject> rulesJson = JSON.parseArray(templateDetail.getItemRules(), JSONObject.class);
                    itemVo.setRules(rulesJson);
                }

                if (AppraiseTemplateItemTypeEnum.RADIO.getCode().equals(templateDetail.getItemType())
                        || AppraiseTemplateItemTypeEnum.CHECKBOX.getCode().equals(templateDetail.getItemType()) ) {
                    CaseAppraiseTemplateSelectVo itemVo1 = new CaseAppraiseTemplateSelectVo();
                    BeanUtils.copyProperties(itemVo, itemVo1);
                    List<JSONObject> jsonObjects = JSON.parseArray(templateDetail.getItemOptions(), JSONObject.class);
                    itemVo1.setOptions(jsonObjects);
                    groupContent.add(itemVo1);
                } else if (AppraiseTemplateItemTypeEnum.INPUT.getCode().equals(templateDetail.getItemType())) {
                    if (!ObjectUtils.isEmpty(templateDetail.getItemAttrs())) {
                        CaseAppraiseTemplateTextareaVo itemVo1 = new CaseAppraiseTemplateTextareaVo();
                        BeanUtils.copyProperties(itemVo, itemVo1);
                        JSONObject attrsJson = JSON.parseObject(templateDetail.getItemAttrs());
                        itemVo1.setAttrs(attrsJson);
                        JSONObject propsJson = JSON.parseObject(templateDetail.getItemProps());
                        itemVo1.setProps(propsJson);
                        itemVo1.setDefaultText(templateDetail.getDefaultText());
                        groupContent.add(itemVo1);
                    } else {
                        groupContent.add(itemVo);
                    }
                } else {
                  continue;
                }
            }
            groupInfoVo.setGroupContent(groupContent);
            templateGroupInfoList.add(groupInfoVo);
        }

        return templateGroupInfoList;
    }


    /**
     * 描述： 获取评价模板初始项目值列表
     *
     * @author: zhengqiang
     * @date: 2023/2/3
     * @param: caseType
     * @return {@link Map}
     **/
    @Override
    public Map getCaseAppraiseTemplateDefaultValueMap(String caseType) {
        Map valueMap = new HashMap();
        Example example = new Example(CaseAppraiseTemplateInfo.class);
        example.createCriteria().andEqualTo("applyCaseType", caseType).andEqualTo("isDelete", Constant.STRING_0);
        List<CaseAppraiseTemplateInfo> caseAppraiseTemplateInfos = caseAppraiseTemplateInfoMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(caseAppraiseTemplateInfos)) {
            throw new BusinessException("案件评价模板不存在");
        }
        CaseAppraiseTemplateInfo caseAppraiseTemplateInfo = caseAppraiseTemplateInfos.get(0);

        Example detailExample = new Example(CaseAppraiseTemplateDetail.class);
        detailExample.createCriteria().andEqualTo("templateId", caseAppraiseTemplateInfo.getId()).andEqualTo("isDelete",Constant.STRING_0);
        detailExample.orderBy("itemSort").asc();
        List<CaseAppraiseTemplateDetail> caseAppraiseTemplateDetails = caseAppraiseTemplateDetailMapper.selectByExample(detailExample);
        if (ObjectUtils.isEmpty(caseAppraiseTemplateDetails)) {
            throw new BusinessException("案件评价模板详情未配置");
        }
        for (CaseAppraiseTemplateDetail caseAppraiseTemplateDetail : caseAppraiseTemplateDetails) {
            valueMap.put("value" + caseAppraiseTemplateDetail.getItemSort(), null);
        }
        return valueMap;
    }
}